package com.yanqu.road.server.cmd.marriage;

import com.yanqu.road.entity.marriage.MarriageInfo;
import com.yanqu.road.entity.player.UserMarriageData;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.pb.MarriagePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.marriage.MarriageProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.MarriageModule;
import com.yanqu.road.server.manger.CrossUnionServerGroupMgr;
import com.yanqu.road.server.manger.marriage.MarriageMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.ConfigHelper;

import java.util.List;


@Cmd(code = Protocol.S_MARRIAGE_REFRESH_SERVER_APPLY_LIST, desc = "刷新全服联姻列表")
public class MarriageRefreshServerApplyCmd extends AbstractCommand {

    private int childSex;

    private List<MarriageInfo> serverApplyList;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        MarriageProto.MarriageRefreshServerApplyReqMsg reqMsg = MarriageProto.MarriageRefreshServerApplyReqMsg.parseFrom(packet.getBytes());
        childSex = reqMsg.getChildSex();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ret = player.getModule(MarriageModule.class).refreshServerApply();
        if(0 == ret){

            //前n次联姻，强制npc,即本服联姻
            UserMarriageData userMarriageData = player.getModule(MarriageModule.class).getUserMarriageData();
            long marriageTimes = 0;
            if(null != userMarriageData) {
                marriageTimes = userMarriageData.getTotalMarriageTimes();
            }

            //如果开启商会分组，则上跨服请求
            long serverId = ConfigHelper.getLong("serverId");
            if(CrossUnionServerGroupMgr.getGroupServerCountByServerId(serverId) > 1 && marriageTimes >= GameConfig.MARRIAGE_CAN_NPC_TIMES){
                MarriageProto.MarriageRefreshServerApplyReqMsg.Builder reqMsg = MarriageProto.MarriageRefreshServerApplyReqMsg.newBuilder();
                reqMsg.setChildSex(childSex);
                player.sendPacket(Protocol.C_CROSS_WORLD_MARRIAGE_REFRESH_APPLY_LIST, reqMsg);
                return;
            }
            serverApplyList = MarriageMgr.refreshServerApplyList(player, childSex);
        }

    }

    @Override
    public void sendData(GamePlayer player) {

        //前n次联姻，强制npc,即本服联姻
        UserMarriageData userMarriageData = player.getModule(MarriageModule.class).getUserMarriageData();
        long marriageTimes = 0;
        if(null != userMarriageData) {
            marriageTimes = userMarriageData.getTotalMarriageTimes();
        }
        if(marriageTimes >= GameConfig.MARRIAGE_CAN_NPC_TIMES){
            return;
        }

        MarriageProto.MarriageRefreshServerApplyRespMsg.Builder respMsg = MarriageProto.MarriageRefreshServerApplyRespMsg.newBuilder();
        respMsg.setRet(ret);
        if(0 == ret) {
            MarriageProto.MarriageApplyListMsg.Builder listMsg = MarriagePb.parseMarriageApplyListMsg(serverApplyList);
            respMsg.setDataList(listMsg);
        }
        player.sendPacket(Protocol.U_MARRIAGE_REFRESH_SERVER_APPLY_LIST, respMsg);
    }
}
